﻿<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp"%>
<%!
/**
 * 更新病人卡号 对应的存储过程名称 -》更新病人卡号
 * @param params
 */
	public void updatePatientCard(Map<String, String> param) {
		Connection connection = null;
		CallableStatement statement = null;
		try {
			connection = getConn();
			statement = connection.prepareCall("{ Call 更新病人卡号(?,?) }"); 
			statement.setString(1, Tools.getDesc(param.get("cardNo"), ""));
			statement.setString(2, Tools.getDesc(param.get("oldCardNo"), ""));
			statement.execute(); 
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
					if (statement != null)
						statement.close(); 
					if (connection != null)
						connection.close(); 
				} catch (Exception e) {
					statement = null;
					connection = null;
					e.printStackTrace();
				}
		}
	}
%>

<%
	Message msg = new Message();
	try {
		String cardNo =  input.getDesc("cardNo","");//病人新卡号
		
		String idCard = input.getDesc("idCard",""); // 身份证号
		
		String oldCardNo = null;
		

		// 根据身份证号查询旧卡号
		Connection connection = null;
		PreparedStatement  statement = null;
		
		try {
			connection = getConn();
			
			//判断新卡号是否已经被绑定
			String sql0="SELECT count(1) as sum  from SF_BRXXK where cardno= ? ";
			statement = connection.prepareStatement(sql0); 
			statement.setString(1, cardNo);
			ResultSet rs0 = statement.executeQuery();
			int sum = 0;
			while(rs0.next()){
         sum = rs0.getInt("sum");
      }
      // sum大于0说明该卡号已经被绑定
			if(sum>0) {
				 msg.setErrcode(FAILURE);
				 msg.setErrmsg("该卡已经被绑定.");
				 out.println(msg);
				 return;
			
			}
			
			
			String sql="SELECT cardno oldCardNo from SF_BRXXK where sfzh= ? ";
			statement = connection.prepareStatement(sql); 
			statement.setString(1, idCard);
			 ResultSet rs = statement.executeQuery();
			while(rs.next()){
         oldCardNo = rs.getString("oldCardNo");
      }
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
					if (statement != null)
						statement.close(); 
					if (connection != null)
						connection.close(); 
				} catch (Exception e) {
					statement = null;
					connection = null;
					e.printStackTrace();
				}
		}
		
		//更新病人卡号
		Map<String, String> param  = new HashMap<String,String>(2);
		param.put("cardNo",cardNo);
		param.put("oldCardNo",oldCardNo);
		updatePatientCard(param);
		msg.put("errcode",0);
    }catch (Exception e){
	   	msg.put("errcode",FAILURE);
    }
		out.println(msg);
%>